﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using viNews.Classes;
using viNews.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viNews_FullView_FullView : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["NewsId"] == null)
            return;
        int newsid = 0;
        if (Int32.TryParse(Request.QueryString["NewsId"], out newsid))
        {
            News news = NewsController.GetNews(newsid);
            if (news == null)
                return;
            TITLE.Text = news.Title;
            AUTHOR.Text = news.Author;
            SUMMARY.Text = news.Summary;
            // SEO            
            WebPageController.AddTitle(Page, news.Title);
            WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Description, WebHelpers.StripHtml(news.Summary));
            WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Author, news.Author);
            List<string> tagNames = TagController.GetTags(news.NewsId).ConvertAll(
                                    new Converter<Tag, string>(GetTagName));
            string keywords = string.Join(",", tagNames.ToArray());
            if (string.IsNullOrEmpty(keywords))
                keywords += CategoryController.GetCategory(news.Category).Name;
            else
                keywords += "," + CategoryController.GetCategory(news.Category).Name;
            WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Keywords, keywords);
            
            DATE.Text = news.Date.ToString("f");
            if (!string.IsNullOrEmpty(news.ImageUrl))
            {
                IMAGE.ImageUrl = WebRoots.GetResourcesRoot + news.ImageUrl;
                IMAGE.Visible = true;
            }            
            DESCRIPTION.Text = NewsController.GetNewsDescription(newsid);
            List<Comment> comments = CommentController.GetComments(news.NewsId);
            foreach (Comment comment in comments)
            {
                //CommentView commentview = (CommentView)LoadControl(WebRoots.GetThemesRoot + SiteSettings.Theme + "/viNews/CommentView.ascx");
                CommentView commentview = (CommentView)LoadControl("CommentView.ascx");
                commentview.Comment = comment;
                COMMENTS.Controls.Add(commentview);
            }
            // show new comment form
            UserControl form = (UserControl)LoadControl(WebRoots.GetModulesRoot + "viNews/FullView/CommentsForm.ascx");
            COMMENTS_FORM.Controls.Add(form);
        }
    }
    private static string GetTagName(Tag tag)
    {
        return tag.Name;
    }
}
